﻿using System;
using System.IO;

namespace sinetd.Utilities
{
    public class Misc
    {
        private static readonly Random _rng = new Random();
        private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        public static string RandomString(int size)
        {
            char[] buffer = new char[size];

            for (int i = 0; i < size; i++)
            {
                buffer[i] = _chars[_rng.Next(_chars.Length)];
            }
            return new string(buffer);
        }

        public static string FileGetContents(string fileName)
        {
            string content = "";
            bool repeat = true;
            while (repeat)
            {
                try
                {
                    using (StreamReader sr = new StreamReader(fileName))
                    {
                        content = sr.ReadLine().Trim();
                    }
                    //content = File.ReadAllText(fileName);
                    repeat = false;
                }
                catch (System.IO.FileNotFoundException)
                {
                    repeat = false;
                }
                catch (System.IO.DirectoryNotFoundException)
                {
                    repeat = false;
                }
                catch (System.IO.IOException)
                {
                    //file in use
                }
            }
            return content;
        }
    }
}
